package com.lujianfei.recyclerview.behavior;

import android.content.Context;
import android.content.res.Resources;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Scroller;

import com.lujianfei.recyclerview.R;

import java.lang.ref.WeakReference;

import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.view.ViewCompat;
import androidx.recyclerview.widget.RecyclerView;

public class ScrollHideTitleBehavior extends CoordinatorLayout.Behavior<View> {

    private static final String TAG = "ScrollHideTitle==";
    private boolean hasRegister;
    private View search = null;
    private int searchViewHeight = 0;
    public ScrollHideTitleBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
        searchViewHeight = context.getResources().getDimensionPixelOffset(R.dimen.search_view_height);
    }

    @Override
    public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) {
        //ViewCompat是一个兼容类,在android5.0之前的API为了实现新的效果
        //避免出错使用ViewCompat.xxxx方法可以解决出现低版本错误的问题
        return (axes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;//判断是否竖直滚动
    }

    @Override
    public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, int dx, int dy, @NonNull int[] consumed, int type) {
        super.onNestedPreScroll(coordinatorLayout,child,target,dx,dy,consumed,type);
        Log.d(TAG,"onNestedPreScroll dx = " + dx + " dy = " + dy);
        // child : AppCompatTextView
        // target : RecyclerView
        if (!hasRegister) {
            search = child;
            RecyclerView recyclerView = (RecyclerView) target;
            recyclerView.removeOnScrollListener(onScrollListener);
            recyclerView.addOnScrollListener(onScrollListener);
            hasRegister = true;
        }
    }

    RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
        }

        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            search.setTranslationY(search.getTranslationY() - dy);
            int completelyHiddenY = -searchViewHeight;
            int completelyShowY = 0;
            if (search.getTranslationY() <= completelyHiddenY) { // 完全隐藏时，处于刚好隐藏的位置不变
                search.setTranslationY(completelyHiddenY);
            } else {
                if (search.getTranslationY() >= completelyShowY){ // 完全显示时，处于刚好完全显示的位置不变
                    search.setTranslationY(completelyShowY);
                }
            }
        }
    };
}
